<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------


namespace app\api\logic;

use app\common\logic\AccountLogLogic;
use app\common\model\{AccountLog, AfterSale, DistributionOrder, NoticeSetting, Order, User};
use app\common\server\{AreaServer, ConfigServer, UrlServer};
use think\Db;
use think\Exception;
use think\facade\Hook;

class ServiceProviderLogic
{

    /**
     * 填写申请记录
     * @param $post
     * @param $user_id
     * @return bool|string
     */
    public static function apple($post, $user_id)
    {
        try {
            $time = time();
            $data = [
                'user_id' => $user_id,
                'type' => $post['type'], // 1-个人, 2-企业
                'real_name' => $post['real_name'],
                'phone' => $post['phone'],
                'province' => $post['province'],
                'city' => $post['city'],
                'district' => $post['district'],
                'id_card' => $post['id_card'], // 身份证
                'id_card_front' => $post['id_card_front'], // 身份证正面
                'id_card_back' => $post['id_card_back'],
                'address' => $post['address'],
                'create_time' => $time,
                'update_time' => $time,
            ];
            if ($post['type'] == 1) {
                $data['email'] = $post['email'];
            }

            if ($post['type'] == 2) {
                $data['company_name'] = $post['company_name'];
                $data['address'] = $post['address'];
                $data['admin_name'] = $post['admin_name'];
                $data['admin_phone'] = $post['admin_mobile'];
                $data['business_license'] = $post['business_license'] ?? '';
            }

            Db::name('user_service')
                ->insert($data);
            return true;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 获取最新申请详情
     * @param $user_id
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function appleDetail($user_id)
    {

        $result = Db::name('user_service')
            ->where('user_id', $user_id)
            ->order('id', 'desc')
            ->find();

        if (empty($result)) {
            return [];
        }

        $result['province'] = AreaServer::getAddress($result['province']);
        $result['city'] = AreaServer::getAddress($result['city']);
        $result['district'] = AreaServer::getAddress($result['district']);
        $result['type_str'] = $result['type'] == 1 ? '个人' : '企业';
        $result['id_card_front'] = UrlServer::getFileUrl($result['id_card_front']);
        $result['id_card_back'] = UrlServer::getFileUrl($result['id_card_back']);
        $result['business_license'] = UrlServer::getFileUrl($result['business_license']);

        switch ($result['is_verified']) {
            case 0:
                $result['status_str'] = '已提交，等待客服审核...';
                break;
            case 1:
                $result['status_str'] = '';
                break;
            case 2:
                $result['status_str'] = '审核失败，请重新提交审核';
                break;
        }
        return $result;
    }

}